using UnityEngine;
using System;
using WolfCarol.AvgToolkit.Save;

namespace WolfCarol.AvgToolkit.Examples
{
    [Serializable]
    public class StageData
    {
        public ScreenplayData ScreenplayData;

        public ImageData[] ImageDatas;

        public AudioData[] AudioDatas;

        public TextData TextData;

        public ChoiceData[] ChoiceDatas;

        public VariableData VariableData;

        public StageData()
        {
            ScreenplayData = new ScreenplayData();
            ImageDatas = new ImageData[0];
            AudioDatas = new AudioData[0];
            TextData = new TextData();
            ChoiceDatas = new ChoiceData[0];
            VariableData = new VariableData();
        }
    }

    public class StagePresenter : MonoBehaviour, ISavable<StageData>
    {
        [SerializeField] private ScreenplayPresenter m_screenplayPresenter;

        [SerializeField] private ImagePresenter m_imagePresenter;

        [SerializeField] private AudioPresenter m_audioPresenter;

        [SerializeField] private Typewriter m_typewriter;

        [SerializeField] private Branch m_branch;

        [SerializeField] private VariableManager m_variableManager;

        private void Awake()
        {
            m_screenplayPresenter = GetComponent<ScreenplayPresenter>();
            m_imagePresenter = GetComponent<ImagePresenter>();
            m_audioPresenter = GetComponent<AudioPresenter>();
            m_typewriter = GetComponent<Typewriter>();
            m_branch = GetComponent<Branch>();
            m_variableManager = GetComponent<VariableManager>();
            m_screenplayPresenter.linePresenter = m_typewriter;
            m_screenplayPresenter.choicePresenter = m_branch;
            m_branch.commandManager = m_screenplayPresenter;
            m_variableManager.commandManager = m_screenplayPresenter;
            m_typewriter.variableManager = m_variableManager;
        }

        public void StartScreenplay(string _name)
        {
            m_screenplayPresenter.Load(new ScreenplayData()
            {
                Path = _name,
                Index = 0,
                CanExecute = true
            });
            m_screenplayPresenter.Next();
        }

        public void Clear()
        {
            m_screenplayPresenter.Clear();
            m_imagePresenter.Clear();
            m_audioPresenter.Clear();
            m_typewriter.Clear();
            m_branch.Clear();
            m_variableManager.Clear();
        }

        public StageData Save()
        {
            return new StageData()
            {
                ScreenplayData = m_screenplayPresenter.Save(),
                ImageDatas = m_imagePresenter.Save(),
                AudioDatas = m_audioPresenter.Save(),
                TextData = m_typewriter.Save(),
                ChoiceDatas = m_branch.Save(),
                VariableData = m_variableManager.Save()
            };
        }

        public void Load(StageData obj)
        {
            m_screenplayPresenter.Load(obj.ScreenplayData);
            m_imagePresenter.Load(obj.ImageDatas);
            m_audioPresenter.Load(obj.AudioDatas);
            m_typewriter.Load(obj.TextData);
            m_branch.Load(obj.ChoiceDatas);
            m_variableManager.Load(obj.VariableData);
        }
    }
}